package com.ymttest.database.sqlwapper;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;

import com.ymt.database.SqlManager;
import com.ymttest.database.mapping.YmtIdCardInfoMapper;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.model.YmtIdCardInfoExample;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name : YmtIdCardInfoWapper.java
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtIdCardInfoWapper {

	SqlManager manage;

	YmtIdCardInfoExample example = new YmtIdCardInfoExample();

	public YmtIdCardInfoExample getYmtIdCardInfoExample() {

		example.clear();

		return example;

	}

	public YmtIdCardInfoWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());
	}

	/**
	 * insert 一条YmtIdCard记录
	 * 
	 * @param record
	 *            insertProducts
	 * @return
	 * @throws InterruptedException
	 */
	public int insertIdCardInfo(YmtIdCardInfo record) {

		YmtIdCardInfoMapper ymtIdCardInfoMapper = manage.getSession()
				.getMapper(YmtIdCardInfoMapper.class);

		int i = ymtIdCardInfoMapper.insertSelective(record);

		manage.commit();

		return i;
	}
	/**
	 * 根據BuyerId 查詢身份證信息
	 * 
	 * @param buyerId
	 * @return
	 * @throws InterruptedException
	 */
	@SuppressWarnings("unchecked")
	public YmtIdCardInfo selectByBuyerId(long userId) {

		example.clear();

		example.createCriteria().andBuyeridEqualTo((int) userId);

		example.setOrderByClause("UploadTime desc");

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						example);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result.get(0);

	}
	/**
	 * 根据BuyerId 查询身份信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	@SuppressWarnings("unchecked")
	public List<YmtIdCardInfo> selectByBuyerIdList(long userId) {

		example.clear();

		example.createCriteria().andBuyeridEqualTo((int) userId)
				.andIsDeleteEqualTo(0);

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						example);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result;

	}
	/**
	 * 根据name,idNumber 查询身份信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public YmtIdCardInfo selectByNameAndId(String name, String cardNumber) {

		Map m = new HashedMap();

		m.put("name", name);
		m.put("IDCard", cardNumber);

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample2",
						m);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result.get(0);

	}

	/**
	 * 根据idNumber 查询身份信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	@SuppressWarnings("unchecked")
	public List<YmtIdCardInfo> selectByCardId(String cardNumber) {

		example.clear();

		example.createCriteria().andCardnumberEqualTo(cardNumber);

		return manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						example);

	}

	/**
	 * update
	 * @param record
	 */
	public void update(YmtIdCardInfo record, YmtIdCardInfoExample example) {

		YmtIdCardInfoMapper ymtIdCardInfoMapper = manage.getSession()
				.getMapper(YmtIdCardInfoMapper.class);

		ymtIdCardInfoMapper.updateByExampleSelective(record, example);

		manage.commit();

	}
	/**
	 * selectByUserId
	 * @param userId
	 * @param status  （1. 通过审核，或未开始审核， 2. 审核不通过）
	 * @param IsDelete 1 已删除，0 未删除
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<YmtIdCardInfo> selectByUserId(long userId, int status,
			boolean hasPic, int IsDelete) {

		example.clear();

		YmtIdCardInfoExample.Criteria criteria = example.createCriteria();

		criteria.andBuyeridEqualTo((int) userId).andStatusEqualTo(status)
				.andIsDeleteEqualTo(IsDelete).andHaspicEqualTo(hasPic);

		example.setOrderByClause("updatetime desc");

		return manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						example);

	}
	/**
	 * 根據name,buyerId 查詢身份證信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public YmtIdCardInfo selectByNameAndBuyerId(String name, long buyerId) {

		Map m = new HashedMap();

		m.put("name", name);
		m.put("buyerId", buyerId);

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample2",
						m);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result.get(0);

	}

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {

//		System.out.println(new YmtIdCardInfoWapper().selectByBuyerId(288484).getRightSide());
		
		YmtIdCardInfoWapper wapper = new YmtIdCardInfoWapper();
		
		YmtIdCardInfoExample example = wapper.getYmtIdCardInfoExample();
		
		example.createCriteria().andBuyeridEqualTo(20238699)
				.andNameEqualTo("testUser");
		
		YmtIdCardInfo record = new YmtIdCardInfo();
		
		record.setStatus(1);
		
		wapper.update(record, example);
	}
}
